<blockquote>
    <p>Db 操作例子 增删改查分页</p>
    <footer>
        <p>以下只是简单例子, 建议新增business业务层</p>
        <p style="color:red;font-size:13px;">注意：当Mapper接口返回值是List数据类型的时候, 需要在xml中指定返回类型</p>
    </footer>
</blockquote>
<blockquote>
    <p>配置文件</p>
    <footer><code>application.properties</code>文件配置在resources下</footer>
</blockquote>
<pre class="code brush:plain">
### 数据库连接 ###
ants.db.driver-class-name=com.mysql.jdbc.Driver
ants.db.url = jdbc:mysql://ali.mysql.com:3306/idle_egg?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false
ants.db.username = root
ants.db.password = Ants#666
ants.db.data-source = druid
</pre>
<blockquote>
    <p>启动配置类</p>
</blockquote>
<pre class="code brush:java">
package com.acxy.idleegg;

import cn.jants.common.annotation.boot.DbConfiguration;
import cn.jants.common.annotation.boot.PropertyConfiguration;
import cn.jants.common.annotation.plugin.EnableSQLMapPlugin;
import cn.jants.common.annotation.service.Application;
import cn.jants.core.startup.JTomcat;

import java.lang.reflect.InvocationTargetException;
import java.util.UUID;

/**
 * IdleEgg应用启动类
 *
 * @author MrShun
 * @version 1.0
 */
@Application
@PropertyConfiguration
@EnableSQLMapPlugin(value = "com/acxy/idleegg/mapper")
public class IdleEggApplication {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        JTomcat.run(IdleEggApplication.class, 80, "/api");
    }
}
</pre>
<blockquote>
    <p>Mapper接口 <code>MemberMapper.java</code></p>
</blockquote>
<pre class="code brush:java">
package com.acxy.idleegg.mapper;


import cn.jants.plugin.sqlmap.annotation.Mapper;
import com.acxy.idleegg.entity.Member;

import java.util.List;

/**
 * @author MrShun
 * @version 1.0.0
 * @Date 2018-02-02
 */
@Mapper
public interface MemberMapper {

    /**
     * 查询会员列表
     *
     * @return
     */
    List&lt;Member> selectList();

    /**
     * 根据openId查询用户信息
     *
     * @param openId 用户openId
     * @return
     */
    Member selectByOpenId(String openId);


    /**
     * 新增用户数据
     *
     * @param member 实体对象
     * @return
     */
    Long insert(Member member);

    /**
     * 修改会员信息
     *
     * @param member 实体对象
     * @return
     */
    Integer updateByOpenId(Member member);

    /**
     * 删除会员信
     *
     * @param openId
     * @return
     */
    Integer deleteByOpenId(String openId);
}
</pre>
<blockquote>
    <p>Sql配置文件 <code>MemberMapper.xml</code></p>
</blockquote>
<pre class="code brush:xml">
&lt;?xml version="1.0" encoding="UTF-8" ?>
&lt;!DOCTYPE mapper PUBLIC "ANTS" "http://doc.jants.cn/dtd/ants-mapper.dtd" >
<!-- 用户-->
<mapper namespace="MemberMapper" >

    <resultType id="member" type="com.acxy.idleegg.entity.Member" />


    <!--数据和实体的对应映射-->
    <sql id="Base_Column_List" >
        open_id as openId,
        account as account,
        nick_name as nickName,
        sex as sex,
        avatar as avatar,
        tel as tel,
        email as email,
        birthday as birthday,
        is_lock as isLock,
        type as type,
        create_time as createTime
    </sql>

    <!-- //TODO 多条件查询,自己扩充条件 -->
    <select id="selectList" resultType="member">
        select <include refid="Base_Column_List"/> from member
    </select>

    <!-- //TODO 多条件删除,自己扩充条件-->
    <delete id="deleteByOpenId">
        delete from member where open_id = #{openId}
    </delete>


    <!-- 根据主键ID查询一条记录-->
    <select id="selectById">
        select <include refid="Base_Column_List"/> from member where id = #{id}
    </select>

    <!-- 插入不为空的字段 -->
    <insert id="insert">
        insert into member
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="id != null" >
                id,
            </if>
            <if test="openId != null" >
                open_id,
            </if>
            <if test="account != null" >
                account,
            </if>
            <if test="nickName != null" >
                nick_name,
            </if>
            <if test="sex != null" >
                sex,
            </if>
            <if test="avatar != null" >
                avatar,
            </if>
            <if test="tel != null" >
                tel,
            </if>
            <if test="email != null" >
                email,
            </if>
            <if test="birthday != null" >
                birthday,
            </if>
            <if test="isLock != null" >
                is_lock,
            </if>
            <if test="type != null" >
                type,
            </if>
            <if test="createTime != null" >
                create_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="id != null" >
                #{id},
            </if>
            <if test="openId != null" >
                #{openId},
            </if>
            <if test="account != null" >
                #{account},
            </if>
            <if test="nickName != null" >
                #{nickName},
            </if>
            <if test="sex != null" >
                #{sex},
            </if>
            <if test="avatar != null" >
                #{avatar},
            </if>
            <if test="tel != null" >
                #{tel},
            </if>
            <if test="email != null" >
                #{email},
            </if>
            <if test="birthday != null" >
                #{birthday},
            </if>
            <if test="isLock != null" >
                #{isLock},
            </if>
            <if test="type != null" >
                #{type},
            </if>
            <if test="createTime != null" >
                #{createTime},
            </if>
        </trim>
    </insert>

    <!-- 根据主键Id删除数据-->
    <delete id="deleteById">
        delete from member where id = #{id}
    </delete>

    <!-- 据主键Id修改字段不为空的数据 -->
    <update id="updateByOpenId">
        update member
        <set>
            <if test="account != null" >
            account = #{account},
            </if>
            <if test="nickName != null" >
            nick_name = #{nickName},
            </if>
            <if test="sex != null" >
            sex = #{sex},
            </if>
            <if test="avatar != null" >
            avatar = #{avatar},
            </if>
            <if test="tel != null" >
            tel = #{tel},
            </if>
            <if test="email != null" >
            email = #{email},
            </if>
            <if test="birthday != null" >
            birthday = #{birthday},
            </if>
            <if test="isLock != null" >
            is_lock = #{isLock},
            </if>
            <if test="type != null" >
            type = #{type},
            </if>
            <if test="createTime != null" >
            create_time = #{createTime},
            </if>
        </set>
        where open_id = #{openId}
    </update>

    <select id="selectByOpenId">
        select <include refid="Base_Column_List"/> from member where open_id = #{openId} limit 1
    </select>
</mapper>
</pre>
<blockquote>
    <p>Controller控制器 <code>MemberController.java</code></p>
</blockquote>
<pre class="code brush:java">
package com.acxy.idleegg.controller;

import cn.jants.common.annotation.action.*;
import cn.jants.common.annotation.service.Autowired;
import cn.jants.common.annotation.service.Tx;
import cn.jants.common.bean.JsonMap;
import cn.jants.common.bean.Page;
import cn.jants.common.exception.TipException;
import cn.jants.plugin.sqlmap.Paging;
import cn.jants.restful.render.Json;
import com.acxy.idleegg.entity.Member;
import com.acxy.idleegg.mapper.MemberMapper;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 会员 控制器
 *
 * @author MrShun
 * @version 1.0
 */
@Controller("member")
public class MemberController {

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 查询会员分页
     *
     * @param index 当前页数
     * @param size  每页大小
     * @return
     */
    @GET("/page")
    public Map page(@Param Integer index, @Param Integer size) {
        Paging.startPage(index, size);
        List<&lt;Member> members = memberMapper.selectList();
        Page page = new Page(members);
        return Json.success(page);
    }

    /**
     * 查询会员信息
     *
     * @param openId
     * @return
     */
    @GET("/query/{openId}")
    public Map query(@PathVariable String openId) {
        Member member = memberMapper.selectByOpenId(openId);
        //其他扩展属性也就是所谓的DTO
        JsonMap jsonMap = new JsonMap(member);
        jsonMap.put("pos", "java");
        return Json.success(member);
    }


    /**
     * 添加会员信息
     *
     * @param member
     * @return
     */
    @POST("/add")
    public Map add(Member member) {
        member.setOpenId(UUID.randomUUID().toString());
        Long returnKey = memberMapper.insert(member);
        return Json.success(returnKey);
    }


    /**
     * 修改会员信息
     *
     * @param openId
     * @param member
     * @return
     */
    @PUT("/update/{openId}")
    public Map update(@PathVariable String openId, Member member) {
        member.setOpenId(openId);
        Integer count = memberMapper.updateByOpenId(member);
        return Json.success(count);
    }


    /**
     * 删除会员
     *
     * @param openId
     * @return
     */
    @DELETE("/delete/{openId}")
    public Map delete(@PathVariable String openId) {
        Integer count = memberMapper.deleteByOpenId(openId);
        return Json.success(count);
    }

    /**
     * 测试事务回滚机制, 数据库格式InnoDB
     *
     * @param openId
     * @return
     */
    @DELETE("/tx/{openId}")
    @Tx
    public Map tx(@PathVariable String openId) {
        Integer count = memberMapper.deleteByOpenId(openId);
        throw new TipException("测试事务回滚机制...");
    }
}
</pre>
<script type="text/javascript">
    $(function () {
        SyntaxHighlighter.highlight();
        $(".toolbar").remove();
    });
</script>